# _*_ coding utf-8 _*_
# george
# time: 2025/1/13上午10:52
# name: file.py
# comment:
from datetime import datetime

from flask import send_from_directory
from sqlalchemy import Table, Select, insert
from common.database import db_connection
from model.project import Project
from model.team import Team
from model.user import User

import app.config.config as myconfig
import logging

from pathlib import Path

file_logger = logging.getLogger("file database log:")
db_session, Base, engine = db_connection()


class File(Base):
    __table__ = Table("file", Base.metadata, autoload_with=engine)

    # 查询所有文件
    def find_all_file(self, page):
        page = int(page)
        if page < 1:
            page = 1
        count = page * myconfig.Config.page_count
        result = db_session.query(File.file_name, File.file_size, File.upload_time, Team.team_name,
                                  Project.project_name, User.username).outerjoin(Project,
                                                                                 File.project_id == Project.id).outerjoin(
            Team,
            File.team_id == Team.id).outerjoin(
            User, File.uploader_id == User.id).order_by(File.upload_time.desc()).limit(count).all()

        return result

    def add_file(self):
        file_data = {
            'file_name': 'ddd.txt',
            'file_size': '20KB',
            'upload_time': datetime.now(),
            'project_id': 1,
            'team_id': 1,
            'uploader_id': 1,
            'file_link': '/user/666/my/ddd.txt'
        }
        for i in range(5):
            file_data["file_name"] = file_data["file_name"] + str(i)
            new_file = File(**file_data)  # 假设 file_data 中的键与 File 模型的属性相匹配
            db_session.add(new_file)
            db_session.commit()
            file_data["file_name"] = 'ddd.txt'

    # 根据关键词查询数据
    def search_file(self, page, keyword):
        page = int(page)
        if page < 1:
            page = 1
        count = page * myconfig.Config.page_count
        file_logger.debug(f"count:{count}")
        result = db_session.query(
            File.file_name, File.file_size, File.upload_time, Team.team_name,
            Project.project_name, User.username
        ).outerjoin(
            Project, File.project_id == Project.id
        ).outerjoin(
            Team, File.team_id == Team.id
        ).outerjoin(
            User, File.uploader_id == User.id
        ).filter(
            File.file_name.like(f"%{keyword}%")
        ).order_by(
            File.upload_time.desc()
        ).limit(count).all()
        return result

    # 获取文章详情
    def get_file_detail(self, file_name):
        print("file_name", file_name)
        return db_session.query(File).filter_by(file_name=file_name).first()

    def get_file(self, filename):
        print(filename)
        pdf_file = db_session.query(File).filter_by(file_name=filename).first()
        return send_from_directory(Path("/Users/f7692281/Desktop/"), str(filename))

    # 按照project进行显示数据
    def showfile_group(self,group_id,page):
        page = int(page)
        if page < 1:
            page = 1
        count = page * myconfig.Config.page_count
        result = db_session.query(File.file_name, File.file_size, File.upload_time, Team.team_name,
            Project.project_name, User.username
        ).outerjoin(
            Project, File.project_id == Project.id
        ).outerjoin(
            Team, File.team_id == Team.id
        ).outerjoin(
            User, File.uploader_id == User.id
        ).filter(
            File.project_id==group_id
        ).order_by(
            File.upload_time.desc()
        ).limit(count).all()
        return result

    # 按照team进行显示数据
    def showfile_team(self,team_id,page):
        page = int(page)
        if page < 1:
            page = 1
        count = page * myconfig.Config.page_count
        result = db_session.query(File.file_name, File.file_size, File.upload_time, Team.team_name,
                                  Project.project_name, User.username
                                  ).outerjoin(
            Project, File.project_id == Project.id
        ).outerjoin(
            Team, File.team_id == Team.id
        ).outerjoin(
            User, File.uploader_id == User.id
        ).filter(
            File.team_id==team_id
        ).order_by(
            File.upload_time.desc()
        ).limit(count).all()
        return result